<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.${packageName}.${table_name}Dao">

	<sql id="Base_Column_List">
		<#list model_column as model>${model.columnName} ${model.changeColumnName}<#if model_has_next>,</#if> </#list>
	</sql>

	<insert id="insert" parameterType="com.model.${packageName}.${table_name}Model">
		INSERT INTO ${table_name_small}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list model_column as model>
			<if test="null != ${model.changeColumnName} ">${model.columnName}<#if model_has_next>,</#if></if>
			</#list>	
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<#list model_column as model>			
			<if test="null != ${model.changeColumnName} "><#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse><#if model_has_next>,</#if></if>		
			</#list>	
		</trim>
	</insert>
	
	<#list model_column as model>	
	<#if model.isPri == "1">
	<delete id="deleteBy${model.capColumnName}">
		DELETE FROM ${table_name_small}
		WHERE
		<trim prefix="" suffix="" suffixOverrides="and"> 
		${model.columnName} =  <#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse> and
		</trim>
	</delete>
	
	<select id="selectBy${model.capColumnName}"
		resultType="com.model.${packageName}.${table_name}Model">
		SELECT
		<include refid="Base_Column_List" />
		FROM ${table_name_small}
		WHERE
		<trim prefix="" suffix="" suffixOverrides="and"> 
		${model.columnName} =  <#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse> and
		</trim>
	</select>
	</#if>
	</#list>
	
	<delete id="delete">
		DELETE FROM ${table_name_small}
		WHERE 
		<trim prefix="" suffix="" suffixOverrides="and"> 
		<#list model_column as model>
			<#if model.isPri == "1">
		${model.columnName}=<#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse><#if model_has_next> and </#if>
			</#if>		
		</#list>	
		</trim>
	</delete>
	
	<update id="update"
		parameterType="com.model.${packageName}.${table_name}Model">
		UPDATE ${table_name_small}
		<set>
		<#list model_column as model>	
			<if test="null != ${model.changeColumnName} ">${model.columnName}=<#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse><#if model_has_next>,</#if></if>				
		</#list>		
		</set>
		WHERE 
		<trim prefix="" suffix="" suffixOverrides="and"> 
		<#list model_column as model>
			<#if model.isPri == "1">
		${model.columnName}=<#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse><#if model_has_next> and </#if>
			</#if>		
		</#list>	
		</trim>
	</update>

	<select id="load" resultType="com.model.${packageName}.${table_name}Model">
		SELECT
		<include refid="Base_Column_List" />
		FROM ${table_name_small}
		WHERE 
		<trim prefix="" suffix="" suffixOverrides="and"> 
		<#list model_column as model>
			<#if model.isPri == "1">
			${model.columnName}=<#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse> and 
			</#if>
		</#list>
		</trim>	
	</select>

	<select id="selectList"
		resultType="com.model.${packageName}.${table_name}Model">
		select * from (SELECT
		<include refid="Base_Column_List" />
		FROM ${table_name_small} t
		WHERE 1=1
		<if test="null != startTime and '' != startTime ">
			and t.create_time >=  <#noparse>#{</#noparse>startTime<#noparse>}</#noparse>
		</if>
		<if test="null != endTime and '' != endTime ">
			and t.create_time &lt;= concat( <#noparse>#{</#noparse>endTime<#noparse>}</#noparse> ,' 23:59:59')
		</if>
		<if test="null != status and '' != status ">
			and t.status = <#noparse>#{</#noparse>status<#noparse>}</#noparse>
		</if>	
		<if test="null != createId and '' != createId ">
			and t.create_Id = <#noparse>#{</#noparse>createId}
		</if>
		<if test="null != deptIds and '' != deptIds ">
			and t.dept_Id in
			 <foreach collection="deptIds" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#{</#noparse>item<#noparse>}</#noparse>
            </foreach>
		</if>		
		<#list model_column as model>
			<#if model.isPri == "1">
		<if test="null != ${model.changeColumnName} and '' != ${model.changeColumnName} ">
			and t.${model.columnName} = <#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse>
		</if>			
			</#if>		
		</#list>	
		) t1 where 1=1
		<if test="null != filter and ''!=filter ">
			<foreach item="item" index="index" collection="filter"
				open="and (" separator="and" close=")">
				<#noparse>${</#noparse>item.field<#noparse>}</#noparse>  like
				"%"<#noparse>#{</#noparse>item.value<#noparse>}</#noparse>"%"
			</foreach>
		</if>
		<choose>
			<when test="null != sort and ''!= sort">
				order by <#noparse>${</#noparse>sort<#noparse>}</#noparse> <#noparse>${</#noparse>order<#noparse>}</#noparse>
			</when>
			<otherwise>
				order by createTime desc
			</otherwise>
		</choose>
		<if test="null != pageRows">
			limit <#noparse>#{</#noparse>pageNum<#noparse>}</#noparse>,<#noparse>#{</#noparse>pageRows<#noparse>}</#noparse>
		</if>
	</select>

	<select id="selectListCount" resultType="java.lang.Integer"
		parameterType="Map">
		select count(1) from (select
		<include refid="Base_Column_List" />
		FROM
		${table_name_small} t
		WHERE 1=1
		<if test="null != startTime and '' != startTime ">
			and t.create_time >= <#noparse>#{</#noparse>startTime<#noparse>}</#noparse>
		</if>
		<if test="null != endTime and '' != endTime ">
			and t.create_time &lt;= concat(<#noparse>#{</#noparse>endTime<#noparse>}</#noparse>,' 23:59:59')
		</if>
		<if test="null != status and '' != status ">
			and t.status = <#noparse>#{</#noparse>status<#noparse>}</#noparse>
		</if>	
		<if test="null != createId and '' != createId ">
			and t.create_Id = <#noparse>#{</#noparse>createId}
		</if>
		<if test="null != deptIds and '' != deptIds ">
			and t.dept_Id in
			 <foreach collection="deptIds" item="item" index="index" open="(" separator="," close=")">
            <#noparse>#{</#noparse>item<#noparse>}</#noparse>
            </foreach>
		</if>		
		<#list model_column as model>
			<#if model.isPri == "1">
		<if test="null != ${model.changeColumnName} and '' != ${model.changeColumnName} ">
			and t.${model.columnName} = <#noparse>#{</#noparse>${model.changeColumnName}<#noparse>}</#noparse>
		</if>			
			</#if>		
		</#list>	
		) t1 where 1=1
		<if test="null != filter and ''!=filter ">
			<foreach item="item" index="index" collection="filter"
				open="and (" separator="and" close=")">
				<#noparse>${</#noparse>item.field<#noparse>}</#noparse> like
				"%"<#noparse>#{</#noparse>item.value<#noparse>}</#noparse>"%"
			</foreach>
		</if>
	</select>
</mapper>